#! /usr/bin/env python3

# sql index ref: https://blog.csdn.net/qq_43842093/article/details/124810789

from package.load import load_tracelog, load_csv
from package.check import check
from package.show import show_summary, show_detail

import sys
import time

def manual(mod=None):
    help_string = '''
    Slabup V1.0.0
    
    load        load tracelog
    check       check memory leak, default is kmalloc
    show        show memory leak information, default is kmalloc
    draw        draw safe and leak dots
    '''
    print(help_string)

if __name__ == "__main__":
    cmd = None
    val = None
    db = None

    if len(sys.argv) > 1:
        cmd = sys.argv[1]
    if len(sys.argv) > 2:
        val = sys.argv[2:]

    if cmd == 'load':
        if val is None: # load 
            load_tracelog()
        elif len(val) == 1:
            if val[0] == '--csv': # load --csv
                load_csv()
            else: # load <log>
                load_tracelog(val[0])
        elif len(val) == 2 and val[0] == '--csv': # load --csv <csv>
            load_csv(val[1])

    elif cmd == 'check':
        if val is None:
            check(None)
        else:
            check(val[0])

    elif cmd == 'show':
        if val is None:
            show_summary()
        elif len(val) == 1:
            show_detail(val[0])

    elif cmd == 'draw':
        try:
            from package.draw import draw_memleak
        except:
            print('import draw failed!')

        if val is not None and len(val) == 1:
            draw_memleak(val[0])
        else:
            print('help')
    else:
        manual()
